﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleRest.WebTest.Handlers;
using SimpleRest.Core.Routing;
using SimpleRest.Core;

namespace SimpleRest.WebTest.Services
{
    public interface ISomething
    {
    }
    [Service("products2")]
    public class MockProducts2Service : MockProductsService
    {
        private readonly ISomething Something;
        public MockProducts2Service(ISomething something)
        {
            this.Something = something;
        }
    }

    [Service("products")]
    public class MockProductsService : ServiceBase
    {
        public MockProductsService()
        {
            // Mapping that will check request querystring for filter criteria and if exists return a strongly typed filter object for parameter input name filter.
            base.AddMapping(new FilterMappingHandler("filter")); 
        }

        private List<Product> Items = new List<Product>()
        {
            Product.GetMock("0"),
            Product.GetMock("1"),
            Product.GetMock("2"),
            Product.GetMock("3"),
            Product.GetMock("4"),
            Product.GetMock("5"),
            Product.GetMock("6"),
            Product.GetMock("7")
        };

        [WebGet(UriTemplate = "?page={page}&pageCount={pageCount}")]
        public OperationResponse<ProductCollection> GetAll(int? page, int? pageCount, Filter filter)
        {
            if (page.HasValue && page.Value > -1 && pageCount.HasValue && pageCount.Value > 0)
            {
                List<Product> products = new List<Product>();

                int startAt = page.Value * pageCount.Value;
                for (int i = startAt; i < startAt + pageCount.Value; i++)
                {
                    if (Items.Count > i)
                        products.Add(Items[i]);
                    else
                        break;
                }
                return new OperationResponse<ProductCollection>(new ProductCollection(products));
            }
            return new OperationResponse<ProductCollection>(new ProductCollection(Items));
        }

        [WebGet(UriTemplate = "{id}")]
        public Product Get(string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return null;
                //return new OperationResponse<Product>(System.Net.HttpStatusCode.NotFound);
            //return new OperationResponse<Product>(item);
            return item;
        }
            
        [WebGet(UriTemplate = "{id}/Price")]
        public OperationResponse<Price> GetPrice(string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return new OperationResponse<Price>(System.Net.HttpStatusCode.NotFound);
            var price = item.Price;
            if (price == null)
                return new OperationResponse<Price>(System.Net.HttpStatusCode.NotFound);
            return new OperationResponse<Price>(price);
        }

        [WebGet(UriTemplate = "{id}/Content")]
        public OperationResponse<Content> GetContent(string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return new OperationResponse<Content>(System.Net.HttpStatusCode.NotFound);
            var content = item.Content;
            if (content == null)
                return new OperationResponse<Content>(System.Net.HttpStatusCode.NotFound);

            return new OperationResponse<Content>(content);
        }

        [WebDelete(UriTemplate = "{id}")]
        public OperationResponse Delete(string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return new OperationResponse(System.Net.HttpStatusCode.NotFound);
            Items.Remove(item);
            return new OperationResponse(System.Net.HttpStatusCode.OK);
        }

        [WebPost(UriTemplate = "")]
        public OperationResponse<Product> Create([HttpBody]Product item)
        {
            if (item == null)
                return new OperationResponse<Product>(System.Net.HttpStatusCode.NoContent);

            item.Id = Guid.NewGuid().ToString();
            Items.Add(item);
            return new OperationResponse<Product>(new Product() { Id = item.Id }, System.Net.HttpStatusCode.Created);
        }

        public override void OperationExecutionException(Operation targetOperation, Exception ex)
        {
            // put a break point here!
            base.OperationExecutionException(targetOperation, ex);
        }
        public override void OperationPostExecution(Operation targetOperation, object value)
        {
            // put a break point here!
            base.OperationPostExecution(targetOperation, value);
        }
        public override void OperationPreExecution(Operation targetOperation)
        {
            // put a break point here!
            base.OperationPreExecution(targetOperation);
        }
    }
}